#!/bin/bash

############################################################################
#
# 远程执行该脚本，请在命令行中直接执行以下命令：
#
#   curl -sSL https://gitee.com/tay3223/biubiubiu/raw/master/k8s/k8s-v1.28.2-rocky94-init.sh | /bin/bash
#
#
############################################################################
#
# 先使用901号脚本初始化好rocky94的机器
# 在rocky94的linux裸机上直接执行该脚本，就可以安装好k8s-1.28.2相关的 <容器运行时三大件> + <kube三剑客> + <crictl>
# 已经处理过脚本逻辑，可以重复多次运行
# 该安装方法没有抄自任何博客，百分百内容来自于1.28.x的k8s官方文档：https://v1-28.docs.kubernetes.io/zh-cn/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
#




#***********************
# 机器初始化
#***********************
#安装k8s必备工具（要安装这些依赖是基于这个版本的镜像为基础才需要安装：Rocky-9-GenericCloud-Base.9.4.x86_64.qcow2）
yum install -y conntrack-tools iptables ebtables socat


#关闭swap并确保重启后不启用
swapoff -a
if grep -q '^[^#].* swap ' /etc/fstab; then
    sed -i '/ swap / s/^/#/' /etc/fstab
fi


#加载br_netfilter模块并确保重启后加载
if ! lsmod | grep -q br_netfilter; then
    modprobe br_netfilter
fi
if ! grep -q 'br_netfilter' /etc/modules-load.d/br_netfilter.conf; then
    echo 'br_netfilter' > /etc/modules-load.d/br_netfilter.conf
fi


#设置网络参数并确保重启后生效
if ! grep -q 'net.bridge.bridge-nf-call-iptables = 1' /etc/sysctl.conf; then

cat <<EOF >> /etc/sysctl.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sysctl -p
fi


# 定义要追加的内容
HOSTS_BLOCK=$(cat << 'EOF'

#由于懒得创建LB代理6443端口，因此这里注册一个域名用来给k8s使用，后期可以把这个dns指向到LB上
192.168.5.230 k8s-master-01.tianye.work

#master节点
192.168.5.231 rocky9-k8s1
192.168.5.232 rocky9-k8s2
192.168.5.233 rocky9-k8s3

#node节点
192.168.5.234 rocky9-k8s4
192.168.5.235 rocky9-k8s5
192.168.5.236 rocky9-k8s6
192.168.5.237 rocky9-k8s7
192.168.5.238 rocky9-k8s8

EOF
)

# 检查并追加到 /etc/hosts
if ! grep -q "192.168.5.235" /etc/hosts; then
    echo "$HOSTS_BLOCK" >> /etc/hosts
fi

# 方法一：cloud-init会重写/etc/hosts，因此在cloud-init的模板文件中也添加一下解析配置，防止下次重启后/hosts丢失
#if ! grep -q "192.168.5.235" /etc/cloud/templates/hosts.redhat.tmpl; then
#    echo "$HOSTS_BLOCK" >> /etc/cloud/templates/hosts.redhat.tmpl
#fi

# 方法二：当然要可以选择把 manage_etc_hosts 设置为 False，默认情况下值是True（细节没时间研究，直接选择方法一了）
# 永久关闭 manage_etc_hosts
if grep -q 'manage_etc_hosts' /etc/cloud/cloud.cfg; then
    sed -i 's/manage_etc_hosts: true/manage_etc_hosts: false/' /etc/cloud/cloud.cfg
else
    echo 'manage_etc_hosts: false' >> /etc/cloud/cloud.cfg
fi


#***********************
# 安装containerd
#***********************
mkdir -p ~/bao && cd ~/bao

#这个速度慢，但是省钱（最好的办法是内网新建一个file服务器，明天再搞）
#wget https://file.taycc.com/d/bao/k8s/v1.28.2/containerd-1.7.23-linux-amd64.tar.gz
#这个速度快，但是每个月都要支付下行流量包的费用
wget http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/containerd-1.7.23-linux-amd64.tar.gz

mkdir -p /opt/websuite/containerd
rm -rf /opt/websuite/containerd/*
tar zxvf ./containerd-1.7.23-linux-amd64.tar.gz -C /opt/websuite/containerd/
mkdir -p /etc/containerd
/opt/websuite/containerd/bin/containerd config default > /etc/containerd/config.toml

#因为下文已经有代理的存在了，所以就不用指定aliyun的镜像地址，也就不会出现版本不一致的问题
sed -i 's|registry.k8s.io/pause:3.8|registry.aliyuncs.com/google_containers/pause:3.9|g' /etc/containerd/config.toml

# 检查配置是否已存在（代理暂时还没有调试通，先用aliyun的源顶着）
#if ! grep -q '\[plugins."io.containerd.grpc.v1.cri".registry.configs."docker.io".proxy\]' /etc/containerd/config.toml; then
#cat <<\EOF >> /etc/containerd/config.toml
#[plugins."io.containerd.grpc.v1.cri".registry.configs."docker.io".proxy]
#  http_proxy = "http://192.168.5.246:7890"
#  https_proxy = "http://192.168.5.246:7890"
#  no_proxy = "localhost,127.0.0.1,.example.com,.tianye.work"
#EOF
#fi

cat <<\EOF > /etc/systemd/system/containerd.service

# Copyright The containerd Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

[Unit]
Description=containerd container runtime  # 服务的描述，说明这是 containerd 容器运行时
Documentation=https://containerd.io        # 服务的文档链接，提供更多信息
After=network.target local-fs.target       # 该服务将在网络和本地文件系统准备好后启动

[Service]
ExecStartPre=-/sbin/modprobe overlay       # 启动服务前尝试加载 overlay 内核模块，失败不影响后续启动
ExecStart=/opt/websuite/containerd/bin/containerd  # 服务启动命令，启动 containerd 进程

Type=notify                               # 服务类型为 notify，表示服务会发送通知以指示其启动状态
Delegate=yes                              # 允许服务使用 cgroup 的委托特性
KillMode=process                          # 结束服务时以进方式结束所有子进程
Restart=always                            # 服务崩溃后自动重启
RestartSec=5                              # 重启服务前等待 5 秒

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity                        # 允许的最大进程数，设置为无限制
LimitCORE=infinity                         # 允许的最大核心转储文件大小，设置为无限制

# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity                          # 任务的最大数量，设置为无限制
OOMScoreAdjust=-999                        # 调整 OOM（Out Of Memory）分数，优先级设置为最低

[Install]
WantedBy=multi-user.target

EOF
systemctl daemon-reload
systemctl enable containerd
systemctl restart containerd




#***********************
# 安装runc
#***********************
mkdir -p ~/bao && cd ~/bao
wget http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/runc-1.1.13.amd64
install -o root -g root -m 755 runc-1.1.13.amd64 /usr/local/sbin/runc




#***********************
# 安装cni
#***********************
mkdir -p ~/bao && cd ~/bao
wget http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/cni-plugins-linux-amd64-v1.5.1.tgz
mkdir -p /opt/cni/bin
rm -rf /opt/cni/bin/*
tar Cxzvf /opt/cni/bin ./cni-plugins-linux-amd64-v1.5.1.tgz




#***********************
# 安装kubeadm+kubelet
#***********************
mkdir -p ~/bao && cd ~/bao
wget "http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/crictl-v1.28.0-linux-amd64.tar.gz"
tar zxvf crictl-v1.28.0-linux-amd64.tar.gz -C /usr/local/bin/
chown root:root /usr/local/bin/crictl

#安装kubeadm和kubelet
RELEASE="v1.28.0"
ARCH="amd64"
cd /usr/local/bin
wget "http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/kubeadm-1.28.2"
mv kubeadm-1.28.2 kubeadm
wget "http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/kubelet-1.28.2"
mv kubelet-1.28.2 kubelet
chmod +x {kubeadm,kubelet}

cat << \EOF > /etc/systemd/system/kubelet.service
[Unit]
Description=kubelet: The Kubernetes Node Agent
Documentation=https://kubernetes.io/docs/
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/local/bin/kubelet
Restart=always
StartLimitInterval=0
RestartSec=10

[Install]
WantedBy=multi-user.target

EOF

mkdir -p /etc/systemd/system/kubelet.service.d
cat << \EOF > /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
# Note: This dropin only works with kubeadm and kubelet v1.11+
[Service]
Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
# This is a file that "kubeadm init" and "kubeadm join" generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically
EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
# This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use
# the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.
EnvironmentFile=-/etc/sysconfig/kubelet
ExecStart=
ExecStart=/usr/local/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS

EOF

systemctl daemon-reload
systemctl enable --now kubelet




#***********************
# 安装kubectl
#***********************
mkdir -p ~/bao && cd ~/bao
wget http://osoc.oss-cn-shanghai.aliyuncs.com/bao/k8s/v1.28.2/kubectl-1.28.2
install -o root -g root -m 0755 kubectl-1.28.2 /usr/local/bin/kubectl
kubectl version --client




cat << EOF

====================================
总共安装了七个东西：

1.容器运行时：
- containerd
- runc
- cni

2.kubeadm相关组件
- kubelet
- kubeadm
- kubectl
- crictl

3.相关命令：
    systemctl daemon-reload
    systemctl restart containerd
    systemctl restart kubelet
====================================

EOF




